

package com.ybwh.springboot2.common.mybatis.plus.page;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import org.springframework.cglib.beans.BeanMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PageInfo<T> implements IPage<T> {
    private List<T> records;
    private long total;
    private long size;
    private long current;
    private String[] ascs;
    private String[] descs;
    private T queryCondition;

    public PageInfo() {
        this.records = Collections.emptyList();
        this.total = 0L;
        this.size = 10L;
        this.current = 1L;
    }

    public PageInfo(long current, long size) {
        this(current, size, 0L);
    }

    public PageInfo(long current, long size, long total) {
        this.records = Collections.emptyList();
        this.total = 0L;
        this.size = 10L;
        this.current = 1L;
        if (current > 1L) {
            this.current = current;
        }

        this.size = size;
        this.total = total;
    }

    public boolean hasPrevious() {
        return this.current > 1L;
    }

    public boolean hasNext() {
        return this.current < this.getPages();
    }


    public List<T> getRecords() {
        return this.records;
    }

    public PageInfo<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    public long getTotal() {
        return this.total;
    }

    public PageInfo<T> setTotal(Long total) {
        this.total = total;
        return this;
    }

    public PageInfo<T> setTotal(long total) {
        this.total = total;
        return this;
    }

    public long getSize() {
        return this.size;
    }

    public PageInfo<T> setSize(long size) {
        this.size = size;
        return this;
    }

    public long getCurrent() {
        return this.current;
    }

    public PageInfo<T> setCurrent(long current) {
        this.current = current;
        return this;
    }

    public String[] ascs() {
        return this.ascs;
    }

    public PageInfo<T> setAsc(String... ascs) {
        this.ascs = ascs;
        return this;
    }

    public String[] descs() {
        return this.descs;
    }

    public PageInfo<T> setDesc(String... descs) {
        this.descs = descs;
        return this;
    }

    public Map<Object, Object> condition() {
        return this.queryCondition == null ? null : new HashMap(beanToMap(this.queryCondition));
    }

    private static Map<String, Object> beanToMap(Object bean) {
        return null == bean ? null : BeanMap.create(bean);
    }

    public void setQueryCondition(T queryCondition) {
        this.queryCondition = queryCondition;
    }

    public T getQueryCondition() {
        return this.queryCondition;
    }


    @Override
    public List<OrderItem> orders() {
        List<OrderItem> orders = new ArrayList();
        String[] ascArr;
        int len;
        int i;
        String desc;
        OrderItem o;
        if (this.ascs != null) {
            ascArr = this.ascs;
            len = ascArr.length;

            for (i = 0; i < len; i++) {
                desc = ascArr[i];
                o = new OrderItem();
                o.setAsc(true);
                o.setColumn(this.filter(desc));
                orders.add(o);
            }
        }

        if (this.descs != null) {
            ascArr = this.descs;
            len = ascArr.length;

            for (i = 0; i < len; i++) {
                desc = ascArr[i];
                o = new OrderItem();
                o.setAsc(false);
                o.setColumn(this.filter(desc));
                orders.add(o);
            }
        }

        return orders;
    }

    private static final Pattern pattern = Pattern.compile("\\s*|\t|\r|\n");
    String[] specialChar = new String[]{"*", "'", ";", "-", "--", "+", "//", "/", "%", "#", "$", "=", "\\", ",", "0x"};

    private String filter(String source) {
        Matcher matcher = pattern.matcher(source);
        String preStr = matcher.replaceAll("");
        String[] sc = specialChar;
        int len = specialChar.length;

        for (int i = 0; i < len; i++) {
            String schar = sc[i];
            if (preStr.contains(schar)) {
                throw new IllegalArgumentException("order Parameters is invalid!");
            }
        }

        return preStr;
    }




}
